<template>
	<view class="content">
		<view class="top3">
			<view>
				<input type="text" placeholder="请输入您的手机号" v-model="uName" class="ren">
			</view>
			<view>
				<input type="password" placeholder="请输入您的密码" v-model="uPass" class="suo">
			</view>
		</view>
		<view class="input-box">
			<button class="loginBtn" @click="login()">登录</button>
		</view>
		<view class="newUser">
			<navigator url="../../pages_jifen/center/updateUserPassword">忘记密码</navigator>
			<text style="color: #D1D1D1;">|</text>
			<navigator url="registerUser">新用户注册</navigator>
		</view>
	</view>
</template>

<script>
	import store from 'vuex'
	export default {
		data() {
			return {
				border:true,	// 是否显示边框
				uName:"",		// 用户名
				uPass:"",		// 密码
				openId:"",		// 小程序openId
			}
		},
		onLoad() {
			this.getOpenId();
		},
		methods: {
			// 开始登录
			login(){
				// this.openId = 'oSJAe5GS7ywJxwLEcB7S7ID0HEN0';
				// uni.setStorageSync('openId','oSJAe5GS7ywJxwLEcB7S7ID0HEN0');
				this.$http.post('/userinfo/loginxcx', {
					username:this.uName,
					password:this.uPass,
					openId:this.openId
				},
				{header:{'content-type':"application/x-www-form-urlencoded"}},
				).then(res => {
					if(res.data.code==200){
						let userid = res.data.data.usersId;
						let type = res.data.count;
						if(type == 0 || type == 2){
							//获取用户信息
							uni.getUserProfile({
								desc: '获取你的昵称、头像、地区及性别',
								success: res => {
									// console.log(res);
									//用户绑定小程序openId
									this.$http.get('/userinfo/userBdWeixinMini',{
										userid:userid,
										openId:this.openId,
										icon:res.userInfo.avatarUrl,
										nick:res.userInfo.nickName,
										type:type
									}).then(res => {
										// console.log(res);
									})
								},
								fail: res => {
									//拒绝授权
									this.$u.toast("您拒绝了请求,不能正常使用小程序购买支付功能")
									return;
								}
							});
						}
						this.$store.state.user = res.data.data;
						this.$store.state.logined=true;
						uni.setStorageSync("usersId",userid);
						uni.setStorageSync("userinfo",res.data.data);
						uni.setStorageSync("logined",true);
						uni.setStorageSync('xcxIcon',res.data.msg);
						//刷新页面数据
						uni.$emit("checkCart");				//携联商城购物车
						uni.$emit("checkUserInfo");			//携联商城个人中心加载用户数据
						uni.$emit('getOrderByGroup');		//携联商城个人中心加载订单数量
						uni.$emit('getJifenCenter');		//积分商城个人中心
						uni.$emit('getJifenCat');			//积分商城购物车
						uni.$emit('init');					//品牌共享首页数据（商品价格）
						uni.$emit('checkPinpaiUserInfo');	//品牌共享个人中心加载用户数据
						uni.$emit('getPinpaiOrderByGroup');	//品牌共享个人中心加载订单状态数量
						uni.$emit('getXingyunUserInfo');	//幸运商城个人中心加载用户数据
						this.$u.toast("登录成功")
						setTimeout(res => {
							// uni.switchTab({
							// 	url:"/pages/center/index"
							// })
							uni.navigateBack({
								
							})
						},500)
					}else{
						// this.$u.toast("用户名或密码错误")
						this.$u.toast(res.data.msg)
					}
				})
			},
			//获取用户openId
			getOpenId(){
				this.openId = uni.getStorageSync('openId');
				// uni.login({
				// 	success: res => {
				// 	    // console.info('临时code：',res.code);//code值(5分钟失效)
				// 		this.$http.get('/userinfo/getOpenid',{
				// 			code:res.code
				// 		}).then(res => {
				// 			// console.log("用户openId："+res.data)
				// 			uni.setStorageSync('openId',res.data)
				// 			this.openId = res.data;
				// 		})
				// 	}
				// })
			},
		}
	}
</script>

<style scoped>
	.top3 {
		width: 90%;
		margin-left: 5%;
	}
	.top {
	    background: #EF6700;
	    width: 90%;
	    height: 45px;
	    color: lightgray;
	    text-align: center !important;
	    margin-top: 8%;
	    margin-left: 5%;
	    font-size: 120%;
	    font-weight: bold;
	    line-height: 40px;
	}
	.content{
		margin-top: 40rpx;
	}
	.input-box{
		margin: 0rpx 20rpx;
		position: relative;
	}
	.input-box .icon{
		position: absolute;
		left: 10rpx;
		top: 20rpx;
		z-index: 100;
	}
	.input-box .input{
		background-color: white;
	}
	.loginBtn{
		width: 95%;
		margin-top: 60rpx;
		background-color: #FF7900;
		color: white;
		letter-spacing:10px;
	}
	.newUser{
		width: 60%;
		margin: auto;
		margin-top: 50rpx;
		color: #3a8ee6;
		display: flex;
		font-size: 37rpx;
		justify-content: space-around;
	}
	.ren{
		border: 0.5rpx #ecdede solid;
		background-color: white;
		height: 50px;
		padding-left: 40px;
		background-image: url(../../static/center/login_name.png);
		background-size: 20px;
		background-repeat: no-repeat;
		background-position-x: 10px;
		background-position-y: 15px;
	}
	.suo{
		border: 0.5rpx #ecdede solid;
		background-color: white;
		height: 50px;
		padding-left: 40px;
		background-image: url(../../static/center/login_password.png);
		background-size: 20px;
		background-repeat: no-repeat;
		background-position-x: 10px;
		background-position-y: 15px;
	}
</style>
